/**
 * 打开新增窗口
 */
function openAdd() {
	var defaultComanyName = $("#defaultCompanyName").val();
	var defaulttaxNo = $("#defaultTaxNo").val();
	$("invoiceId").val('');
	$("#companyName").val(defaultComanyName);
	$("#taxNo").val(defaulttaxNo);
	$("#registeAddress").val('');
	$("#registePhone").val('');
	$("#openBankName").val('');
	$("#bankAccount").val('');
	$('#invoicebox').show();
}

/**
 * 打开公司发票窗口
 */
function doEditInvoice(invoiceId) {
	if (invoiceId) {
		$.ajax({
			type : "post",
			url : rootPath + '/pc/my/companyInvoice/getCompanyInvoiceById',
			data : {
				invoiceId : invoiceId
			},
			async : false,
			datatype : "json",
			success : function(json) {
				if (json.resultCode == '1' && json.companyInvoice) {
					var companyInvoice = json.companyInvoice;
					var companyInfo = json.company
					$("#invoiceId").val(companyInvoice.id);
					$("#companyName").val(companyInvoice.companyName ? companyInvoice.companyName : companyInfo.companyName);
					$("#taxNo").val(companyInvoice.taxNo ? companyInvoice.taxNo : companyInfo.taxNo);
					$("#registeAddress").val(companyInvoice.registeAddress);
					$("#registePhone").val(companyInvoice.registePhone);
					$("#openBankName").val(companyInvoice.openBankName);
					$("#bankAccount").val(companyInvoice.bankAccount);
					$('#invoicebox').show();
				} else {
					layer.msg(json.resultMsg, {
						icon : 2,
						time : 2000
					});
				}
			},
			error : function() {
				layer.msg('获取发票信息错误', {
					icon : 2,
					time : 2000
				});
			}
		});
	} else {
		layer.msg('获取发票信息错误', {
			icon : 2,
			time : 1000
		});
	}
}

/**
 * 设置默认发票
 */
function setDefault(invoiceId) {
	if (invoiceId) {
		$.ajax({
			type : "post",
			url : rootPath + '/pc/my/companyInvoice/doSetDefault',
			data : {
				invoiceId : invoiceId
			},
			async : false,
			datatype : "json",
			success : function(json) {
				if (json.resultCode == '1') {
					layer.msg(json.resultMsg,{icon : 1,time:500}, function() {
						window.location.href = rootPath + "/pc/my/companyInvoice";
					});
				} else {
					layer.msg(json.resultMsg, {
						time : 2000
					});
				}
			},
			error : function() {
				layer.msg('设置默认发票信息失败', {
					time : 1000
				});
			}
		});
	} else {
		layer.msg('请先选择发票信息', {
			time : 1000
		});
	}
}

/**
 * 删除发票信息
 */
function doDelInvoice(invoiceId) {
	if(!invoiceId) {
		return;
	}
	$("#invoiceId").val(invoiceId);
	$('#del_box').show();
}

/**
 * 删除发票信息
 */
function delInvoice() {
	var invoiceId = $("#invoiceId").val();
	//信息框-例2
	$.ajax({
		type : "post",
		url : rootPath + '/pc/my/companyInvoice/doDelCompanyInvoice',
		data : {
			invoiceId : invoiceId
		},
		async : false,
		datatype : "json",
		success : function(json) {
			if (json.resultCode == '1') {
				$('#del_box').hide();
				layer.msg(json.resultMsg,{icon : 1,time:500}, function() {
					window.location.href = rootPath + "/pc/my/companyInvoice";
				});
			} else {
				layer.msg(json.resultMsg, {
					time : 2000
				});
			}
		},
		error : function() {
			layer.msg('删除失败', {
				time : 1000
			});
		}
	});
}

/**
 * 校验表单
 */
function doCheckInvoice() {
	var taxNoPatrn = /^[0-9a-zA-Z]+$/;
	var isNumPatrn = /^(-)?\d+(\.\d+)?$/;
	var companyName = $("#companyName").val();
	var taxNo = $("#taxNo").val();
	var bankAccount = $("#bankAccount").val();
	if (!companyName) {
		layer.msg('单位名称必须填写', {
			icon : 2,
			time : 1000
		});
		return false;
	}if (companyName.length > 100) {
		layer.msg('单位名称最多100个字符', {
			icon : 2,
			time : 1000
		});
		return false;
	}
	if (!taxNo) {
		layer.msg('税号必须填写', {
			icon : 2,
			time : 1000
		});
		return false;
	}
	if (taxNo.length < 3) {
		layer.msg('纳税人识别码至少3个字符', {
			icon : 2,
			time : 1000
		});
		return false;
	}
	if (taxNo.length > 20) {
		layer.msg('纳税人识别码最多20个字符', {
			icon : 2,
			time : 1000
		});
		return false;
	}
	if (!taxNoPatrn.test(taxNo)) {
		layer.msg('税号只能是数字或字母', {
			icon : 2,
			time : 1000
		});
		return false;
	}
	if (bankAccount) {
		if (!isNumPatrn.test(bankAccount)) {
			layer.msg('银行账户只能是数字', {
				icon : 2,
				time : 1000
			});
			return false;
		}
	}
	if (bankAccount.length > 30) {
		layer.msg('银行账户最多30个字符', {
			icon : 2,
			time : 1000
		});
		return false;
	}
	return true;
}